/* 
 *  This file is part of hmvcBlog-dashboard.
 *
 *  hmvcBlog-dashboard is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-dashboard is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-dashboard.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblogentry;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.client.applicationframe.events.in.AddApplicationViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblogentry.events.in.ShowAddBlogEntryViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.BlogEntryNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.BlogEntryAsyncCallback;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.events.out.BlogEntrySuccessfullAddEvent;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerModelViewRegistry;

/**
 * 
 *
 * @author jens.meiss
 */
public class AddBlogEntryController extends AControllerModelViewRegistry<AddBlogEntryModel, AddBlogEntryView> {

	public void init() {
		initView();
		register(ShowAddBlogEntryViewEvent.class);
	}

	public void initView() {
		
		Label labelTitle = new Label(I18N.constants.labelBlogEntryTitle());
		getView().setLabelTitle(labelTitle);
		
		Label labelText = new Label(I18N.constants.labelBlogEntryText());
		getView().setLabelTitle(labelText);
		
		TextBox textBoxTitle = new TextBox();
		getView().setTextBoxTitle(textBoxTitle);
		
		TextArea textAreaText = new TextArea();
		getView().setTextAreaText(textAreaText);
		
		Button buttonAddEntry = new Button(I18N.constants.buttonAddBlogEntry());
		getView().setButtonAddEntry(buttonAddEntry);
		buttonAddEntry.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				addBlogEntry();
			}
		});
		
		Button buttonCancel = new Button(I18N.constants.buttonCancel());
		getView().setButtonAddEntry(buttonCancel);
		buttonCancel.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				getComponent().terminateComponent();
				History.back();
			}
		});
		
		HorizontalPanel horizontalPanelButtons = new HorizontalPanel();
		horizontalPanelButtons.add(buttonAddEntry);
		horizontalPanelButtons.add(buttonCancel);
		getView().setHorizontalPanelButtons(horizontalPanelButtons);
		
		Grid gridContainer = new Grid(3,2);
		getView().setGridContainer(gridContainer);
		
		gridContainer.setWidget(0, 0, labelTitle);
		gridContainer.setWidget(0, 1, textBoxTitle);
		gridContainer.setWidget(1, 0, labelText);
		gridContainer.setWidget(1, 1, textAreaText);
		
		gridContainer.setWidget(2, 1, horizontalPanelButtons);
	}

	private void addBlogEntry() {
	
		BlogEntryNewDTO blogEntryNewDTO = new BlogEntryNewDTO();
		blogEntryNewDTO.setBlogId(getModel().getBlogId());
		blogEntryNewDTO.setText(getView().getTextAreaText().getText());
		blogEntryNewDTO.setTitle(getView().getTextBoxTitle().getText());
		
		BlogEntryAsyncCallback.add(blogEntryNewDTO, this);
	}
	
	public void terminate() {
		
	}

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == ShowAddBlogEntryViewEvent.class) {
			fireEventGlobal(new AddApplicationViewEvent(null, getView().getWidget()));
		} else if (event.getClass() == BlogEntrySuccessfullAddEvent.class) {
			History.newItem(HistoryToken.editBlog + "=" + getModel().getBlogId());
			getComponent().terminateComponent();
		}
		
	}

	public void initModel() {
	}

}
